package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import vn.sunnet.backviet01.game.coloa.constant.Constants;
import vn.sunnet.backviet01.game.coloa.interfaces.MyEnemy;
import vn.sunnet.backviet01.game.coloa.util.MyAnimatedSprite;

public class Enemy extends MyAnimatedSprite implements MyEnemy {
	public int name;
	protected int status;

	protected boolean isListenSoundAurra;

	protected boolean isCriptical;

	protected long timeFreezed;
	protected boolean isFreezed;

	protected long timeStun;
	protected long startStun;
	protected boolean isStun;

	protected float init_velocity;

	protected float ySort;

	protected float xBottom;
	protected float yBottom;

	protected float xContain;
	protected float yContain;

	protected float widthContain;
	protected float heightContain;

	protected float agility;
	protected float intelligent;
	protected float streng;

	protected float hitPoints;
	protected float manaPoints;

	protected float mTotalHitPoint;

	protected float armour = 0;
	protected float attackBasic;

	protected boolean isPoisonous;
	protected boolean isBlazed;

	protected boolean isBloodLine = false;

	protected int tiledAttack = -1;

	protected float EXP;

	protected boolean isAllowChangeStatus;
	protected boolean isMoveBack;
	protected boolean isShot;
	protected boolean life;
	protected boolean isAttack = false;
	protected boolean isDie = false;

	protected boolean isFireSkill = false;

	protected long mFrameDurationEach_Attack;
	protected long mFrameDurationEach_Die;

	public PhysicsHandler handler;
	public BloodLine mBloodLine;

	protected final TiledTextureRegion mTextureRegionMove;
	protected TiledTextureRegion mTextureRegion_next;

	public Enemy(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
		this.mTextureRegionMove = pTiledTextureRegion;
		handler = new PhysicsHandler(this);
		this.registerUpdateHandler(handler);
		this.tiledAttack = tiledAttack;
		resetInformation();
		createContain();
	}

	public boolean isListenSoundAurra() {
		return isListenSoundAurra;
	}

	public void setListenSoundAurra(boolean isListenSoundAurra) {
		this.isListenSoundAurra = isListenSoundAurra;
	}

	public void setVelocity(float velocity) {
		init_velocity = velocity;
		handler.setVelocityX(velocity);
	}

	public void setVelocity(float velocity, boolean isReset) {
		handler.setVelocityX(velocity);
	}

	public void setTimeStun(long stun) {
		timeStun = +stun;
	}

	public void setStun(boolean isStun) {
		if (isFreezed) return;
		this.isStun = isStun;
	}

	public boolean isStun() {
		return isStun;
	}

	protected void createContain() {
		xContain = this.getX() + 10;
		yContain = this.getY() + 10;

		widthContain = this.getWidth() - 20;
		heightContain = this.getHeight() - 20;
	}

	public boolean isFreezed() {
		return isFreezed;
	}

	public void setFreezed(boolean isFreezed) {
		this.isFreezed = isFreezed;
	}

	public void setTimeFreezed(long time) {
		this.timeFreezed = time;
	}

	public float getTimeFreezed() {
		return timeFreezed;
	}

	public void setWidthContain(float pWidth) {
		this.widthContain = pWidth;
	}

	public void setHeightContain(float pHeight) {
		this.heightContain = pHeight;
	}

	public void setFireSkill(boolean isFireSkill) {

	}

	protected void onManagedUpdate(float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		this.xBottom = this.getX() + this.getWidth() / 2;
		this.yBottom = this.getY() + this.getHeight() - 15;
	}

	public TiledTextureRegion getmTextureRegion_next() {
		return mTextureRegion_next;
	}

	public void setYSort(float ySort) {
		this.ySort = ySort;
	}

	public float getYSort() {
		return this.ySort;
	}

	public boolean isDie() {
		return this.isDie;
	}

	public void setmTextureRegion_next(TiledTextureRegion mTextureRegion_next) {
		this.mTextureRegion_next = mTextureRegion_next;
	}

	public boolean isAttack() {
		return isAttack;
	}

	public float getBottomX() {
		return this.xBottom;
	}

	public float getBottomY() {
		return this.yBottom;
	}

	public void setPosition(float pX, float pY) {
		super.setPosition(pX, pY);
		this.ySort = pY + this.getHeight();
	}

	public void setAttack(boolean isAttack) {
		this.isAttack = isAttack;
	}

	public boolean isLife() {
		return life;
	}

	public void setLife(boolean life) {
		this.life = life;
	}

	public void reduceBlood(float blood) {
		if (isShot) {
			this.hitPoints = hitPoints - blood + blood * armour * 0.007f;
			isShot = false;
		} else {
			this.hitPoints = hitPoints - blood;
		}
			
	}

	public float getxContain() {
		return xContain;
	}

	public void setxContain(float xContain) {
		this.xContain = xContain;
	}

	public float getyContain() {
		return yContain;
	}

	public void setyContain(float yContain) {
		this.yContain = yContain;
	}

	public float getWidthContain() {
		return widthContain;
	}

	public float getHeightContain() {
		return heightContain;
	}

	public void setEXP(float exp) {
		this.EXP = exp;
	}

	public float getEXP() {
		return EXP;
	}

	public void setHP(float hitPoints) {
		this.hitPoints = hitPoints;
		this.mTotalHitPoint = hitPoints;
	}

	public float getHP() {
		return hitPoints;
	}

	public void setMana(float mana) {
		this.manaPoints = mana;
	}

	public float getMana() {
		return manaPoints;
	}

	public void setArmour(float armour) {
		this.armour = armour;
	}

	public float getArmour() {
		return armour;
	}

	public void setAttackBasic(float attackBasic) {
		this.attackBasic = attackBasic;
	}

	public float getAttackBasic() {
		return attackBasic;
	}

	public void moveBack(boolean moveBack) {
		this.isMoveBack = moveBack;
	}

	public boolean isMoveBack() {
		return isMoveBack;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public long getmFrameDurationEach_Attack() {
		return mFrameDurationEach_Attack;
	}

	public void setmFrameDurationEach_Attack(long mFrameDurationEach_Attack) {
		this.mFrameDurationEach_Attack = mFrameDurationEach_Attack;
	}

	public long getmFrameDurationEach_Die() {
		return mFrameDurationEach_Die;
	}

	public void setmFrameDurationEach_Die(long mFrameDurationEach_Die) {
		this.mFrameDurationEach_Die = mFrameDurationEach_Die;
	}

	@Override
	public void shot(boolean isShot) {
		this.isShot = isShot;

	}

	@Override
	public boolean isShot(final float pX, final float pY) {
		if (xContain < pX && xContain + widthContain > pX && yContain < pY
				&& yContain + heightContain > pY) {
			isShot = true;
			return true;
		}

		return false;
	}

	@Override
	public void allowChangeStatus(boolean isAllow) {
		this.isAllowChangeStatus = isAllow;

	}

	@Override
	public boolean isAllowChangeStatus() {
		return isAllowChangeStatus;
	}

	@Override
	public void resetInformation() {
		this.isShot = false;
		this.life = true;
		this.isMoveBack = false;
		this.isAllowChangeStatus = false;
		this.status = Constants.MOVE;
		mBloodLine = null;
		this.isBloodLine = false;
	}

	@Override
	public int getName() {
		return this.name;
	}

	@Override
	public void setName(int name) {
		this.name = name;
	}

	@Override
	public float getInitHP() {
		return mTotalHitPoint;
	}

	@Override
	public boolean isBloodLine() {
		return isBloodLine;
	}

	@Override
	public void setBloodLine(BloodLine pBloodLine) {
		isBloodLine = true;
		mBloodLine = pBloodLine;
	}

	public static enum EnemyStates {
		MOVE, ATTACK, DIE, FREEZED;
	}

}
